<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>
  <body>
    <!-- <div class="divClass" id="divId">divText</div> -->
    <!-- <div class="divClass" id="divId">
      <p>111</p>
      <span>222</span>
    </div> -->

    <script>
      const obj = {
        nodeName: "div",
        parentNode: document.body,
        attr: { class: "divClass", id: "divId" },
        children: [
          {
            nodeName: "p",
            attr: { class: "pClass", id: "pId" },
            children: "hello",
          },
          {
            nodeName: "span",
            attr: { class: "spanClass", id: "spanId" },
            children: "world",
          },
        ],
      }
      const h = ({ nodeName, parentNode, attr, children }) => {
        const $el = document.createElement(nodeName)
        for (i in attr) {
          $el.setAttribute(i, attr[i])
        }
        if (typeof children === "string") {
          const dom = document.createTextNode(children)
          $el.appendChild(dom)
        } else {
          for (let item = 0; item < children.length; item++) {
            const element = h(children[item])
            $el.appendChild(element)
          }
        }
        parentNode && parentNode.appendChild($el)
        return $el
      }
      const observe = (str) => {
        if (str === "change") {
          console.log("change")
          h(obj)
        }
      }
      const handler = {
        set(target, property, value, receiver) {
          target[property] = value
          observe("change")
          return true
        },
        get(target, property, receiver) {
          observe("change")
          return target
        },
      }
      const proxyVirtualDom = new Proxy(obj, handler)
      proxyVirtualDom.nodeName = "h2"
    </script>
  </body>
</html>
